package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.dto.SysConfigDto;
import com.cluster.platform.mapper.SysConfigMapper;
import com.cluster.platform.model.SysConfig;
import com.cluster.platform.service.SysConfigService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: SysConfigServiceImpl
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    SysConfigMapper sysConfigMapper;

    @Override
    @Transactional
    public CommonResult<Object> batchSysConfig(Integer status, List<String> ids) {
        if (null == status || ids.isEmpty()) {
            return CommonResult.failed("未获取参数信息");
        }
        int batchUpdate = sysConfigMapper.batchSysConfig(status, ids);
        if (batchUpdate<0) {
            return CommonResult.failed("操作失败");
        }
        return CommonResult.success(batchUpdate);
    }

    @Override
    public CommonResult<Object> fetchList(SysConfigDto sysConfigDto) {
        if (null == sysConfigDto.getPageNum() || null == sysConfigDto.getPageSize()) {
            return CommonResult.failed("未获取分页参数信息");
        }
        PageHelper.startPage(sysConfigDto.getPageNum(), sysConfigDto.getPageSize());
        SysConfig sysConfig = BeanUtil.copyProperties(sysConfigDto, SysConfig.class);
        List<SysConfig> sysConfigs = sysConfigMapper.searchSysConfig(sysConfig);
        if (sysConfigs.isEmpty()) {
            CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(sysConfigs));
    }

    @Override
    public CommonResult<Object> searchSysConfig(SysConfig sysConfig) {
        List<SysConfig> sysConfigs = sysConfigMapper.searchSysConfig(sysConfig);
        if (sysConfigs.isEmpty()) {
            return CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(sysConfigs);
    }

    @Override
    @Transactional
    public CommonResult<Object> addSysConfig(SysConfig sysConfig) {
        if (null == sysConfig) {
            return CommonResult.failed("未获取参数信息");
        }
        sysConfig.setId(UUID.randomUUID().toString().replace("-", ""));
        sysConfig.setInsertDate(new Date());
        sysConfig.setStatus(1);
        int insertSelective = sysConfigMapper.insertSelective(sysConfig);
        if (insertSelective < 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(sysConfig);
    }

    @Override
    @Transactional
    public CommonResult<Object> updateSysConfig(SysConfig sysConfig) {
        sysConfig.setUpdateDate(new Date());
        int update = sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(sysConfig);
    }

    @Override
    @Transactional
    public CommonResult<Object> delSysConfig(SysConfig sysConfig) {
        if (null == sysConfig) {
            return CommonResult.failed("未获取参数信息");
        }
        sysConfig.setStatus(0);
        sysConfig.setUpdateDate(new Date());
        int update = sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
        if (update < 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success(sysConfig);
    }
}
